#include "durationslider.h"
#include "utils.h"
#include <QHBoxLayout>
#include <QDebug>
#include <QMouseEvent>

DurationSlider::DurationSlider(QWidget *parent)
    : QWidget(parent)
{
    slider = new QSlider(Qt::Horizontal);   // 水平
    slider->setObjectName("slider");
    slider->setRange(0, 180);               // 设置范围
    slider->installEventFilter(this);       // 加载父窗口时间分发器
    slider->setFocusPolicy(Qt::NoFocus);

    timeLabel = new QLabel("00:00/00:00", this);

    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->addWidget(slider);
    mainLayout->addWidget(timeLabel);

    // 以对象名启用槽
    QMetaObject::connectSlotsByName(this);

    /* 设置qss */
    setStyleSheet(getQss());
}

QString DurationSlider::getQss()
{
    return "QLabel{ \
                color:rgb(255, 255, 255); \
            } \
            QSlider::add-page:Horizontal \
            { \
                background-color: rgba(255, 255, 255, 150); \
                height:4px; \
            } \
            QSlider::sub-page:Horizontal \
            { \
                background-color: rgb(255, 255, 255); \
                height:4px;\
            } \
            QSlider::groove:Horizontal \
            { \
                background:transparent; /* 背景透明 */ \
                height:4px; \
            } \
            /* 中间的“点”的图标 */ \
            QSlider::handle:Horizontal \
            { \
                height:16px; \
                width:16px; \
                border-image:url(:/images/sliderHandle.png); \
                margin: -6 0px; \
            }";
}

bool DurationSlider::eventFilter(QObject *watched, QEvent *event)
{
    if ((event->type() == QEvent::MouseButtonPress) && (watched == slider))
    {
        on_slider_mouseLButtonPress(watched, event);
    }
    return QWidget::eventFilter(watched, event);
}

void DurationSlider::on_slider_mouseLButtonPress(QObject *slider, QEvent *event)
{
    // 只处理 鼠标左键 的按下事件
    QSlider     * sliderCtrl = static_cast< QSlider     * >(slider);
    QMouseEvent * mouseEvent = static_cast< QMouseEvent * >(event );
    if (Qt::LeftButton != mouseEvent->button())
        return;

    // 确定控件操作的基本参数
    int cxctl = 0;  // 滑块宽度
    int cxwnd = 0;  // 滑槽长度
    int mxpos = 0;  // 鼠标按下的位置

    if (Qt::Horizontal == sliderCtrl->orientation())
    {
        // 水平样式的 slider
        cxctl = sliderCtrl->minimumSizeHint().width();
        cxwnd = sliderCtrl->width();
        // 显示方式是否反向,invertedAppearance=false时,水平的Slider由左向右数值增大,否则反过来。
        if (sliderCtrl->invertedAppearance())
            mxpos = cxwnd - mouseEvent->x();
        else
            mxpos = mouseEvent->x();
    }
    else
    {
        // 垂直样式的 slider
        cxctl = sliderCtrl->minimumSizeHint().height();
        cxwnd = sliderCtrl->height();
        if (sliderCtrl->invertedAppearance())
            mxpos = mouseEvent->y();
        else
            mxpos = cxwnd - mouseEvent->y();
    }

    if (cxwnd <= cxctl)
        return;

    // 计算结果，并设置新计算得到的 position 值
    int scpos = sliderCtrl->minimum() +
                (int)((sliderCtrl->maximum() - sliderCtrl->minimum()) *
                      ((mxpos - cxctl / 2.0) / (cxwnd - cxctl)));

    if (sliderCtrl->sliderPosition() == scpos)
        return;

    sliderCtrl->setSliderPosition(scpos);
}

void DurationSlider::on_slider_valueChanged(int val)
{
    int second  = val % 60;
    int minute = val / 60;

    QString mediaDuration;
    mediaDuration.clear();

    if (minute >= 10)
        mediaDuration = QString::number(minute, 10);
    else
        mediaDuration = "0" + QString::number(minute, 10);

    if (second >= 10)
        mediaDuration = mediaDuration
                + ":" + QString::number(second, 10);
    else
        mediaDuration = mediaDuration
                + ":0" + QString::number(second, 10);

    mediaDuration.append("/").append("03:00");
    timeLabel->setText(mediaDuration);
}

void DurationSlider::on_slider_sliderReleased()
{
    //seek操作
}
